Relayout Hard Drive 2

Current Root/SWAP LVM Partitions

The current disk layout looks like this.

lsblk -o NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT

NAME                 TYPE    SIZE FSTYPE      MOUNTPOINT
nvme0n1              disk  476.9G             
├─nvme0n1p1          part    512M vfat        /boot/efi
├─nvme0n1p2          part    732M ext4        /boot
├─nvme0n1p3          part     75G crypto_LUKS 
│ └─nvme0n1p3_crypt  crypt    75G LVM2_member 
│   ├─vglinux-swap   lvm       9G swap        [SWAP]
│   └─vglinux-root   lvm      66G ext4        /
└─nvme0n1p4          part  400.7G crypto_LUKS 
  └─mydata           crypt 400.7G ext4        /DATA

Backup and Mount Root/SWAP LVM Partition

Backup the / partition (which is in the same partion as SWAP). I do this before replacing the OS so I can refer back to anything if required.

dd if=/dev/nvme0n1p3 of=nvme0n1p3.dd bs=4M

For easy reference unpack /etc and /home/ onto a USB stick or handy other location.

Backup /boot and /boot/efi Paritions

Backup /boot and /boot/efi Paritions.

dd if=/dev/nvme0n1p1 of=nvme0n1p1.dd bs=4M
dd if=/dev/nvme0n1p2 of=nvme0n1p2.dd bs=4M

Partitions should now be safe in case it goes horribly wrong!

Install new OS in /

The idea is we install and test a new OS (non encrypted) on a USB thumb drive. When we are happy with it we wipe nvme0n1p3 and copy the / from the USB drive to nvme0n1p3. Tweak fstab, crypttab and update grub. What could go wrong?

Decrypt and original LUKS partition nvme0n1p3.

cryptsetup open /dev/nvme0n1p3 nvme0n1p3_crypt

Use vgdisplay to see the details

vgdisplay

  --- Volume group ---
  VG Name               vglinux
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  10
  VG Access             read/write
  VG Status             resizable

Use lvscan to see the details

lvscan

  LV     VG      Attr       LSize  
  root   vglinux -wi-ao---- 65.98g                                                    
  swap   vglinux -wi-ao----  9.00g

We should be doing this from the non-encrypted USB installed OS so there should not be a clash of encryption names.

Activate the LVM

vgchange --activate y vglinux

Check.

ls -l /dev/mapper

nvme0n1p3_crypt -> ../dm-0
vglinux-root -> ../dm-2
vglinux-swap -> ../dm-1

Mount the filesystem.

mount /dev/mapper/vglinux-root /media/root

Mount the /boot and /boot/efi.

mount /dev/nvme0n1p2 /media/root/boot
mount /dev/nvme0n1p1 /media/root/boot/efi

Delete / on nvme0n1p3

Delete everything in / on the original nvme0n1p3 parition. This will also delete the contents of nvme0n1p1 and nvme0n1p2 since they are mounted. Note the swap partition is not affected.

rm -fr /media/root/*

We have the backup nvme0n1p3.dd file saved away safely somewhere.

Copy USB new OS / to

Use rsync to copy the OS from the USB / to nvme0n1p3 /

rsync -avAXH \
  --exclude=/dev/* \
  --exclude=/proc/* \
  --exclude=/sys/* \
  --exclude=/tmp/* \
  --exclude=/run/* \
  --exclude=/mnt/* \
  --exclude=/media/* \
  --exclude=/lost+found \
  / /media/root/

Note stuff like /dev /tmp /proc and the like will be re-created when the system boots.

Bind Runtime Directories

Bind the runtime directories so we can create a chroot jail to work on the new OS in nvme0n1p3 /

mount --bind /dev /mnt/dev
mount --bind /proc /mnt/proc
mount --bind /sys /mnt/sys
mount --bind /run /mnt/run

The --bind command binds the original current OS dir over the extrnal media and is then used in the chroot jail.

Chroot Jail

Create a chroot jail to work on the new OS in nvme0n1p3 /

chroot /media/root

Update Grub

First we need to tweak the new /etc/fstab. Easiest is copy is back from the backed up /etc/fstab.

/dev/mapper/vglinux-root    /               ext4    errors=remount-ro  0   1
/dev/nvme0n1p2              /boot           ext4    defaults           0   2
/dev/nvme0n1p1              /boot/efi       vfat    umask=0077         0   1
/dev/mapper/vglinux-swap    none            swap    sw                 0   0

Note I am using /dev references rather than UUIDs because on my laptop these are more immutable than UUIDs!

Next we need to check /etc/default/grub

GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=`( . /etc/os-release && echo ${NAME} )`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=/dev/mapper/vglinux-swap"
GRUB_CMDLINE_LINUX=""
GRUB_BACKGROUND="/boot/laptop0.png"
GRUB_CMDLINE_LINUX=""
...

Recreate grub config and install.

grub-install
update-grub

Update Initramfs

Check /etc/crypttab

nvme0n1p3_crypt /dev/nvme0n1p3 none luks,discard

Note make sure cryptsetup-initramfs is installed

Check /etc/initramfs-tools/conf.d/resume

RESUME=/dev/mapper/vglinux-swap

Update initramfs.

update-initramfs -u

If it fails to boot and dumps at a grub> prompt

Don't pinic! This can happen generating a new grub from USB to EFI.

ls

(hd0) (hd0,gpt1) (hd0,gpt2) (hd0,gpt3)

ls (hd0,gpt2)/
ls (hd0,gpt2)/grub/

configfile (hd0,gpt2)/grub/grub.cfg

The config file should work and boot the system. Once inside the system regenerate grub and initramfs

grub-install
update-grub

update-initramfs -u

Devuan installed. Job done.